package cn.harry.modular.sys.controller;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.R;
import cn.harry.common.enums.BusinessType;
import cn.harry.modular.sys.domain.SysDept;
import cn.harry.modular.sys.service.SysDeptService;
import cn.harry.modular.sys.vo.TreeSelect;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门表
 *
 * @author honghh Date 2020-03-16 08:51:37 Copyright (C) www.tech-harry.cn
 */
@Tag(name = "Sys-dept => 部门管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("sys/dept")
public class SysDeptController {

	private final SysDeptService sysDeptService;

	@Operation(summary = "list 根据关键字获取部门列表")
	@PreAuthorize("@ss.hasPermission('system:dept:list')")
	@GetMapping(value = "/list")
	public R<List<SysDept>> list(Map<String, Object> params) {
		List<SysDept> list = sysDeptService.selectDeptList(params);
		return R.success(list);
	}

	@Operation(summary = "{id} 根据ID获取详细信息")
	@PreAuthorize("@ss.hasPermission('system:dept:query')")
	@GetMapping(value = "/{id}")
	public R<SysDept> getInfo(@PathVariable Long id) {
		return R.success(sysDeptService.selectById(id));
	}

	@Operation(summary = "新增")
	@PreAuthorize("@ss.hasPermission('system:dept:add')")
	@SysLog(title = "部门管理", businessType = BusinessType.INSERT)
	@PostMapping
	public R<Integer> create(@RequestBody SysDept sysDept) {
		return sysDeptService.createDept(sysDept) ? R.success() : R.failed();
	}

	@Operation(summary = "更新")
	@PreAuthorize("@ss.hasPermission('system:dept:edit')")
	@SysLog(title = "部门管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public R<Integer> update(@RequestBody SysDept sysDept) {
		return sysDeptService.updateDept(sysDept) ? R.success() : R.failed();
	}

	@Operation(summary = "删除")
	@PreAuthorize("@ss.hasPermission('system:dept:remove')")
	@SysLog(title = "部门管理", businessType = BusinessType.DELETE)
	@DeleteMapping(value = "/{id}")
	public R<Integer> delete(@PathVariable Long id) {
		return sysDeptService.deleteDeptById(id) ? R.success() : R.failed();
	}


	@Operation(summary = "roleDeptTreeselect/{roleId} 加载对应角色部门列表树")
	@GetMapping(value = "/roleDeptTreeselect/{roleId}")
	public R<Map<String, Object>> roleDeptTreeselect(
			@PathVariable("roleId") Long roleId) {

		List<SysDept> depts = sysDeptService.selectDeptList(new HashMap<>());
		Map<String, Object> map = new HashMap<>();
		map.put("checkedKeys", sysDeptService.selectDeptListByRoleId(roleId));
		map.put("depts", sysDeptService.buildDeptTreeSelect(depts));
		return R.success(map);
	}

	@Operation(summary = "treeselect 获取部门下拉树列表")
	@GetMapping(value = "/treeselect")
	public R<List<TreeSelect>> treeselect(Map<String, Object> params) {
		List<SysDept> depts = sysDeptService.selectDeptList(params);
		return R.success(sysDeptService.buildDeptTreeSelect(depts));
	}

	@Operation(summary = "excludeChild 查询部门列表（排除节点）")
	@GetMapping("/list/exclude/{id}")
	public R<List<SysDept>> excludeChild(
			@PathVariable(value = "id", required = false) Long id) {
		List<SysDept> list = sysDeptService.list();
		list.removeIf(d -> d.getId().intValue() == id || ArrayUtils
				.contains(StringUtils.split(d.getAncestors(), ","), String.valueOf(id)));
		return R.success(list);
	}

}
